package com.sca.auth.interceptor;

import com.sca.auth.annotation.RequireToken;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * @author liuxianfei
 * @since 2020/11/3 10:04
 */
public class TokenInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
        RequireToken requireToken = AnnotationUtils.findAnnotation(method, RequireToken.class);
        if (requireToken != null) {
            System.out.println("is not null");
        }
        if (method.isAnnotationPresent(RequireToken.class)) {
            System.out.println("is present");
        }
        return true;
    }

}
